<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="badCode">
              <el-input v-model="formData.badCode" placeholder="请输入编号" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="badName">
              <el-input v-model="formData.badName" placeholder="请输入名称" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="badType">
              <el-select v-model="formData.badType" placeholder="请选择类别" style="width: 100%">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BAD_TYPE)" 
                          :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" 
                         :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as BadReasonApi from '@/api/quality/badreason';
import { getDictDatas, DICT_TYPE } from '@/utils/dict';

export default {
  name: "BadReasonForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        badCode: undefined,
        badName: undefined,
        badType: undefined,
        status: 1, // 默认开启状态
        remark: undefined,
      },
      // 表单校验
      formRules: {
        badCode: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        badName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        badType: [{ required: true, message: '类别不能为空', trigger: 'change' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await BadReasonApi.getBadReason(id);
          this.formData = res.data;
          this.dialogTitle = "编辑不良代码定义";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增不良代码定义";
      }
    },

    /** 获取字典数据 */
    getDictDatas(dictType) {
      return this.$store.getters.dict_datas[dictType] || [];
    },

    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await BadReasonApi.updateBadReason(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await BadReasonApi.createBadReason(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },

    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        badCode: undefined,
        badName: undefined,
        badType: undefined,
        status: 1, // 默认开启状态
        remark: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
